<template>
  <div class="playlist">
    <div class="title">
      <h1>推荐歌单</h1>
      <div class="more" @click="toPlaylist">
        <span>更多</span><van-icon class="icon" name="arrow" />
      </div>
    </div>
    <div class="wrapper" ref="wrapper">
      <ul class="content" ref="content">
        <li
          v-for="item in playlistData"
          :key="item.content_id"
          @click="selectPlayList(item)"
        >
          <div class="cover-num">
            <img :src="item.picUrl" alt="" />
            <span>{{ (item.playCount / 10000) | numFilter(1) }}万</span>
          </div>
          <span>{{ item.name }}</span>
        </li>
      </ul>
    </div>
  </div>
</template>

<script>
import BScroll from "better-scroll";
import axios from "axios";

export default {
  data() {
    return {
      playlistData: [],
    };
  },
  created() {
    this.playlistData;
  },
  methods: {
    getPlayList() {
      axios
        .get("api/personalized?limit=6", "")
        .then((res) => {
          console.log(res.data.result);
          this.playlistData = res.data.result;
          this.betterScrollHorizontal(this.playlistData.length, 135);
        })
        .catch((err) => {
          console.error(err);
        });
    },
    betterScrollHorizontal(num, itemWidth) {
      let width = num * itemWidth;
      this.$refs.content.style.width = width + "px";
      this.$nextTick(() => {
        this.scroll = new BScroll(this.$refs.wrapper, {
          click: true,
          startX: 0,
          scrollX: true, //横向为true
          scrollY: false,
        });
      });
    },
    selectPlayList(item) {
      const id = item.content_id;
      this.$router.push({
        path: `/playlistdetail/${id}`,
      });
    },
    toPlaylist() {
      this.$router.push("/playlist");
    },
  },
  mounted() {
    this.getPlayList();
  },
  filters: {
    numFilter(value, num) {
      return parseFloat(value).toFixed(num);
    },
  },
};
</script>

<style scoped>
.playlist {
  width: 94%;
  margin: 0 auto;
}
.title {
  text-align: left;
}
.title > h1 {
  display: inline-block;
  font-size: 18px;
  text-align: left;
}
.more {
  float: right;
  position: relative;
  top: 14px;
  border-radius: 20px;
  border: #666666 1px solid;
  padding: 2px 5px;
  color: #666666;
  font-size: 8px;
}
.more > .icon {
  position: relative;
  top: 1px;
}
.wrapper {
  width: 100%;
  overflow: hidden;
}
ul.content {
  text-align: left;
}
ul.content > li {
  width: 120px;
  display: inline-block;
  text-align: center;
  vertical-align: top;
  margin-right: 15px;
  cursor: pointer;
}
ul.content:nth-last-child(1) {
  margin-right: 0;
}
ul.content > li img {
  width: 100%;
  border-radius: 10px;
}
ul.content > li > span {
  text-align: left;
  display: -webkit-box;
  width: 100%;
  font-size: 14px;
  overflow: hidden;
  height: 40px;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  line-height: 20px;
  text-overflow: ellipsis;
  margin: 0 auto;
}
.cover-num {
  position: relative;
}
.cover-num > span {
  position: absolute;
  right: 0px;
  bottom: 4px;
  z-index: 100;
  background: rgba(0, 0, 0, 0.5);
  color: #fff;
  border-radius: 10px;
  display: block;
  font-size: 12px;
  padding: 0px 5px;
}
</style>
